package com.gitee.minimalismstyle.fresh.common.core.validate

import com.gitee.minimalismstyle.fresh.common.core.exception.MessageException
import java.util.regex.Pattern

class RegexValueValidate : ValueValidate {

    override fun check(value: Any?, params: String?): ValidateResult {
        return if(value == null || value.equals("")){
            ValidateResult(true)
        } else if(params == null || params.equals("")){
            throw MessageException(message = "请定义正则表达式")
        } else {
            val isMatch: Boolean = Pattern.matches(params, value.toString())
            if(isMatch){
                ValidateResult(true)
            }else{
                ValidateResult(false, "字符不合法")
            }
        }
    }
}